package com.able.concurrency.t4;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * @author abel.he
 * @data 2018/10/25 15:47
 * @describe
 * ArrayBlockingQueue有界容器
 *  add() 超出队列数量，报错
 *  put() 超出队列数量，自动阻塞
 *  offer()
 *  1参数： 超出队列数量，超出部分不存到队列中
 *  3参数： offer(value,times,timeunit)容量不足的时候，阻塞 times 时长（单位为 timeunit），如果在阻塞时长内，有容量空闲，新增数据返回 true。如果阻塞时长范围内，无容量空闲，放弃新增数据，返回 false
 */
public class Test5_ArrayBlokingQueue {

    final BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(3);

    public static void main(String[] args) {

        final Test5_ArrayBlokingQueue arrayBlokingQueue = new Test5_ArrayBlokingQueue();

        for (int i = 0; i < 5; i++) {
//            System.out.println("add method: " + arrayBlokingQueue.blockingQueue.add("value: " + i));
           /*
           try {
                arrayBlokingQueue.blockingQueue.put("value: " + i);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("put method: " + i);
            */

            try {
                arrayBlokingQueue.blockingQueue.offer("value: " + i, 1, TimeUnit.SECONDS);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("offer method: " + i);
        }

        System.out.println(arrayBlokingQueue.blockingQueue);

    }

}
